package main

import (
	"fmt"
	"sync"
	"time"
)

func main() {
	// 类似于Java的CountDownLatch
	wg := sync.WaitGroup{}

	// 启动多个协程
	for i := 0; i < 5; i++ {
		// 计数器加1
		wg.Add(1)
		go func(index int) {
			fmt.Println("协线程>>>begin, ", index)
			time.Sleep(100 * time.Millisecond)
			// 计数器减1
			defer wg.Done()
			// 异步任务逻辑
			fmt.Println("协线程<<<end, ", index)
		}(i)
	}
	fmt.Println("主线程等待")
	wg.Wait()
	fmt.Println("主线程结束")

}
